﻿using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using SedionFormDesigner.CusControls.TextBox;
using SedionFormDesigner.Infrastructure;

namespace SedionFormDesigner.CusControls.TypeEditor
{
    public class FormularEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null && context.Instance != null)
            {
                return UITypeEditorEditStyle.Modal;
            }
            return base.GetEditStyle(context);
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;
            BD_TextBox txtbox = new BD_TextBox();
            CustomProperty cp = context.Instance as CustomProperty;
            if (cp != null)
                txtbox = cp.GetCurrentSelectObject() as BD_TextBox;

            Control form = txtbox.Parent;
            var ctrls = form.Controls;
            foreach (var ctrl in ctrls)
            {
                if (ctrl.GetType() == typeof(BD_TextBox))
                {
                    var textbox = (BD_TextBox)ctrl;
                    if (textbox.Name == String.Empty)
                    {
                        MessageBox.Show("存在为命名文本框,请确认");
                        return base.EditValue(context, provider, value);
                    }
                }
            }

            if (context != null && context.Instance != null)
            {
                String formular = value == null ? null : value.ToString();
                FrmFormular frmFormular = new FrmFormular(txtbox, formular);
                if (frmFormular.ShowDialog() == DialogResult.OK)
                {
                    value = frmFormular.getFormular();
                    return value;
                }
            }

            return base.EditValue(context, provider, value);
        }
    }
}
